package com.maysahar;

import java.util.HashMap;

import android.app.SearchManager;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.provider.BaseColumns;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

  public static final String TABLE_ENTRIES = "entries";
  public static final String COLUMN_ID = "entryId";
  public static final String COLUMN_NAME = "name";
  public static final String COLUMN_VALUE = "value";
  public static final String COLUMN_SETTING = "setting";
  public static final String COLUMN_VERSION= "version";
  public static final String COLUMN_MODIFIED_ON = "modifiedOn";

  private static final String DATABASE_NAME = "maysahar.db";
  private static final int DATABASE_VERSION = 2;

  private static final HashMap<String,String> mColumnMap = buildColumnMap();

  // Database creation sql statements
  private static final String ENTRIES_CREATE = "create table "
      + TABLE_ENTRIES + "(" + 
      COLUMN_ID + " integer primary key autoincrement, " + 
      COLUMN_NAME + " text not null," +
      COLUMN_VALUE + " text not null," +
      COLUMN_SETTING + " integer not null," +
      COLUMN_VERSION + " integer not null," +
      COLUMN_MODIFIED_ON + " text not null" +
      		");";

  public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(ENTRIES_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(DatabaseHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ENTRIES);
    onCreate(db);
  }

  /**
   * Performs a database query.
   * @param selection The selection clause
   * @param selectionArgs Selection arguments for "?" components in the selection
   * @param columns The columns to return
   * @return A Cursor over all rows matching the query
   */
  public Cursor query(String selection, String[] selectionArgs, String[] columns) {
      /* The SQLiteBuilder provides a map for all possible columns requested to
       * actual columns in the database, creating a simple column alias mechanism
       * by which the ContentProvider does not need to know the real column names
       */
      SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
      builder.setTables(DatabaseHelper.TABLE_ENTRIES);
      builder.setProjectionMap(mColumnMap);

      Cursor cursor = builder.query(getReadableDatabase(),
              columns, selection, selectionArgs, null, null, null);

      if (cursor == null) {
          return null;
      } else if (!cursor.moveToFirst()) {
          cursor.close();
          return null;
      }
      return cursor;
  }


  
  /**
   * Builds a map for all columns that may be requested, which will be given to the 
   * SQLiteQueryBuilder. This is a good way to define aliases for column names, but must include 
   * all columns, even if the value is the key. This allows the ContentProvider to request
   * columns w/o the need to know real column names and create the alias itself.
   */
  private static HashMap<String,String> buildColumnMap() {
      HashMap<String,String> map = new HashMap<String,String>();
      map.put(COLUMN_ID, COLUMN_ID);
      map.put(COLUMN_NAME, COLUMN_NAME);
      map.put(COLUMN_VALUE, COLUMN_VALUE);
      map.put(COLUMN_SETTING, COLUMN_SETTING);
      map.put(COLUMN_VERSION, COLUMN_VERSION);
      map.put(COLUMN_MODIFIED_ON, COLUMN_MODIFIED_ON);
      map.put(BaseColumns._ID, COLUMN_ID + " AS " +
              BaseColumns._ID);
      map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " +
              SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
      map.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS " +
              SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
      return map;
  }

  
} 
